#include "pjsipwrapper.h"

PjApiBuddyManager::PjApiBuddyManager()
{
}

PjApiBuddyManager::~PjApiBuddyManager()
{
}

int PjApiBuddyManager::addBuddy(std::string uri, bool subscribe, std::string desc)
{
    pjsua_buddy_config cfg;
    pjsua_buddy_config_default(&cfg);

    pj_strset2(&cfg.uri, (char*)uri.c_str());

    if(subscribe)
    {
        cfg.subscribe = PJ_TRUE;
    }
    else
    {
        cfg.subscribe = PJ_FALSE;
    }

    //pj_strset2(cfg.user_data, (char*)desc.c_str());
    cfg.user_data = (char*)desc.c_str();
    pj_status_t status;
    pjsua_buddy_id id;

    status = pjsua_buddy_add(&cfg, &id);

    if((status != PJ_SUCCESS) ||(pjsua_buddy_is_valid(id) != PJ_TRUE))
    {
        return -1;
    }

    return id;
}

void PjApiBuddyManager::deleteBuddy(std::string uri)
{
    int id = getBuddyIDByUri(uri);
    pj_status_t status;

    if(isValidBuddyID(id))
    {
        status = pjsua_buddy_del(id);
        if(status != PJ_SUCCESS)
        {
            //pjsua_error();
            return;
        }
    }
}

void PjApiBuddyManager::updateBuddyPresence(std::string uri)
{
    pjsua_buddy_id id = pjsua_buddy_find(&pj_str((char*)uri.c_str()));
    if(id == PJSUA_INVALID_ID)
    {
        //pjsua_error();
        return;
    }

    pj_status_t status = pjsua_buddy_update_pres(id);

    if(status != PJ_SUCCESS)
    {
        // pjsua_error();
        return;
    }
}

int PjApiBuddyManager::getBuddyNumber()
{
    return pjsua_get_buddy_count();
}

bool PjApiBuddyManager::isValidBuddyID(int id)
{
    if(pjsua_buddy_is_valid(id) == PJ_TRUE)
    {
        return true;
    }
    return false;
}

std::string PjApiBuddyManager::getBuddyUriByID(int buddy_id)
{
    if(!isValidBuddyID(buddy_id)) return "";

    pjsua_buddy_info info;
    pj_status_t status = pjsua_buddy_get_info(buddy_id, &info);
    if(status != PJ_SUCCESS)
    {
        //pjsua_error();
        return "";
    }
    return std::string(info.uri.ptr);
}

int PjApiBuddyManager::getBuddyIDByUri(std::string uri)
{
    pjsua_buddy_id id = pjsua_buddy_find(&pj_str((char*)uri.c_str()));
   // pjsua_buddy_id id = pjsua_buddy_find(pj_str((char*)uri.c_str()));
    if(id == PJSUA_INVALID_ID)
    {
        return -1;
    }
    return id;
}

std::string PjApiBuddyManager::getDescriptionByUri(std::string uri)
{
    return "";
}
std::string PjApiBuddyManager::getBuddyStatusTextByUri(std::string uri)
{
    int id = getBuddyIDByUri(uri);
    if( id == -1)
    {
        //pjsua_error;
        return "";
    }

    pjsua_buddy_info info;
    pj_status_t status = pjsua_buddy_get_info(id, &info);
    if(status != PJ_SUCCESS)
    {
        //pjsua_error();
        return "";
    }
    return std::string(info.status_text.ptr);
}

std::string PjApiBuddyManager::getBuddyStatusTextByID(int buddy_id)
{
    if(!isValidBuddyID(buddy_id)) return "";

    pjsua_buddy_info info;
    pj_status_t status = pjsua_buddy_get_info(buddy_id, &info);
    if(status != PJ_SUCCESS)
    {
        //pjsua_error();
        return "";
    }

    return std::string(info.status_text.ptr);
}

enum PjApiBuddyManager::BuddyStatus PjApiBuddyManager::getBuddyStatusByUri(std::string uri)
{
    int id = getBuddyIDByUri(uri);
    if( id == -1)
    {
        //pjsua_error;
        return PjApiBuddyManager::Unknown;
    }

    pjsua_buddy_info info;
    pj_status_t status = pjsua_buddy_get_info(id, &info);
    if(status != PJ_SUCCESS)
    {
        //pjsua_error();
        return PjApiBuddyManager::Unknown;
    }

    switch(info.status)
    {
        case PJSUA_BUDDY_STATUS_UNKNOWN:
            return PjApiBuddyManager::Unknown;
        case PJSUA_BUDDY_STATUS_ONLINE:
            return PjApiBuddyManager::Online;
        case PJSUA_BUDDY_STATUS_OFFLINE:
            return PjApiBuddyManager::Offline;
        default:
            break;
    };
    return PjApiBuddyManager::Unknown;
}

enum PjApiBuddyManager::BuddyStatus PjApiBuddyManager::getBuddyStatusByID(int buddy_id)
{
    if(!isValidBuddyID(buddy_id)) return PjApiBuddyManager::Unknown;

    pjsua_buddy_info info;
    pj_status_t status = pjsua_buddy_get_info(buddy_id, &info);
    if(status != PJ_SUCCESS)
    {
        //pjsua_error();
        return PjApiBuddyManager::Unknown;
    }

    switch(info.status)
    {
        case PJSUA_BUDDY_STATUS_UNKNOWN:
            return PjApiBuddyManager::Unknown;
        case PJSUA_BUDDY_STATUS_ONLINE:
            return PjApiBuddyManager::Online;
        case PJSUA_BUDDY_STATUS_OFFLINE:
            return PjApiBuddyManager::Offline;
        default:
            break;
    };
    return PjApiBuddyManager::Unknown;
}

